package com.common.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtils {

    private static String key="iikiki";

    /**
     * 获得token*
     * * */
    public static String getToken(long id, Date date){
        Map<String, Object> map = new HashMap<>();
        map.put("userId",id);
        String token = Jwts.builder().setClaims(map)
                .signWith(SignatureAlgorithm.HS256, key)
                .setExpiration(date)
                .compact();
        return token;
    }


    /**
     * 解析token获得id
     * * * */
    public static long parserToken(String token){
        Claims body = Jwts.parser().setSigningKey(key)
                .parseClaimsJws(token)
                .getBody();

        return  body.get("userId",Long.class);
    }
}
